package datastructure.find;

/**
 * 二分查找
 * @author 
 * @date 2019/10/3112:54
 */
public class BinarySearch {
    public static void main(String[] args) {
        //必须是有序的
        int arr[] = {2, 5, 7, 8, 10, 22, 22};
        int i = binarySearch(arr, 0, arr.length - 1, 8);
        System.out.println(i);
    }

    private static int binarySearch(int[] arr, int left, int right, int target) {
        while (left<right){
            int mid=left+(right-left)/2;
            if (arr[mid]<target){
                left=mid+1;
            }else if (arr[mid]>target){
                right=mid-1;
            }else {
                return mid;
            }
        }
        return -1;
    }


}